package com.markhsiu.MiniMQ.model;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Result {

	private Message message;
	private Integer error = 0;
	private final CountDownLatch downLatch = new CountDownLatch(1);
	
	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	
	public Integer getError() {
		return error;
	}
	public void setError(Integer error) {
		this.error = error;
	}
	public Message getMessageResult(long timeout, TimeUnit unit) {
		try {
            downLatch.await(timeout, unit);
		} catch (InterruptedException e) {
			throw new RuntimeException();
		}finally {
			downLatch.countDown();
		}
		
		if (error == 1) {
			return null;
		}
		return message;
	}
	
	@Override
	public String toString(){
		return String.format("error:%s message:%s ",
				error,
				message.toString());
	}


    public void release() {
        downLatch.countDown();
    }

}
